package com.powernode.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.domain.Prod;
import com.powernode.domain.ProdComm;
import com.powernode.domain.User;
import com.powernode.feign.ProdToMemberFeign;
import com.powernode.service.ProdCommService;
import com.powernode.mapper.ProdCommMapper;
import com.powernode.service.ProdService;
import com.powernode.vo.ProdCommData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class ProdCommServiceImpl extends ServiceImpl<ProdCommMapper, ProdComm>
        implements ProdCommService {

    @Autowired
    private ProdCommMapper prodCommMapper;

    @Autowired
    private ProdService prodService;

    @Autowired
    private ProdToMemberFeign prodToMemberFeign;

    /**
     * 手动分页
     * select from
     *
     * @param page
     * @param prodComm
     * @return
     */

    @Override
    public Page<ProdComm> getProdCommPage(Page<ProdComm> page, ProdComm prodComm) {
        //进行状态和全局搜索
        prodCommMapper.selectPage(page, new LambdaQueryWrapper<ProdComm>()
                .eq(!ObjectUtils.isEmpty(prodComm.getStatus()), ProdComm::getStatus, prodComm.getStatus()));
        //商品id另外进行分页

        //获取请求信息
        List<ProdComm> records = page.getRecords();
        //获得搜索的商品名称
        String prodName = prodComm.getProdName();
        if (records.size() > 0) {
            //获取到所有的id
            List<Long> prodIds = records.stream().map(comm -> comm.getProdId()).collect(Collectors.toList());
            if (StringUtils.hasText(prodName)) {
                //查询出所以与名字模糊的值
                List<Prod> list = prodService.list(new LambdaQueryWrapper<Prod>()
                        .like(Prod::getProdName, prodName)
                        .in(Prod::getProdId, prodIds));

                //转为Map格式
                Map<Long, Prod> prodMap = list.stream().collect(Collectors.toMap(Prod::getProdId, prod -> prod));
                List<ProdComm> comms = records.stream().filter(f -> prodMap.containsKey(f.getProdId()))
                        .collect(Collectors.toList());
                comms.forEach(r -> {
                    Prod prod = prodMap.get(r.getProdId());
                    r.setProdName(prod.getProdName());
                });
                page.setRecords(comms);
            } else {//没有以商品名称过滤  此时查询商品名称
                //查询id范围内的数据
                List<Prod> prodList = prodService.list(new LambdaQueryWrapper<Prod>()
                        .in(Prod::getProdId, prodIds)
                );
                //建立 id 和对应的对象的关系
                Map<Long, Prod> prodMap = prodList.stream().collect(Collectors.toMap(Prod::getProdId, prod -> prod));
                records.forEach(r -> {
                    Prod prod = prodMap.get(r.getProdId());
                    r.setProdName(prod.getProdName());
                });
            }
        }

        return page;
    }

    /**
     * 获取商品评论条数
     *
     * @param prodId
     * @return
     */
    @Override
    public ProdCommData getProdCommData(Long prodId) {
        //获取到商品的所有评论
        List<ProdComm> prodCommList = prodCommMapper.selectList(new LambdaQueryWrapper<ProdComm>()
                .eq(ProdComm::getProdId, prodId));
        ProdCommData prodCommData = new ProdCommData();
        //此商品可能没有评论
        if (CollectionUtil.isNotEmpty(prodCommList)) {
            //评论总数
            int total = prodCommList.size();
            //好评
            long good = prodCommList.stream().filter(prodComm -> prodComm.getEvaluate().equals(0)).collect(Collectors.toList()).size();
            //中评
            long secondary = prodCommList.stream().filter(prodComm -> prodComm.getEvaluate().equals(1)).collect(Collectors.toList()).size();
            //差评
            long bad = prodCommList.stream().filter(prodComm -> prodComm.getEvaluate().equals(2)).collect(Collectors.toList()).size();
            //有图 pics不为null
            long picCount = prodCommList.stream().filter(prodComm -> ObjectUtils.isEmpty(prodComm.getPics())).count();
            //好评率 四舍五入
            BigDecimal multiply = new BigDecimal(good).divide(new BigDecimal(total), RoundingMode.HALF_UP).multiply(new BigDecimal(100));
            prodCommData.setNumber(total);
            prodCommData.setPraiseNumber(good);
            prodCommData.setSecondaryNumber(secondary);
            prodCommData.setNegativeNumber(bad);
            prodCommData.setPicNumber(picCount);
            prodCommData.setPositiveRating(multiply);
        }
        return prodCommData;
    }

    /**
     * 查询每个下面的评论
     * evaluate 为-1 为总数 0 好评 1 中 2差 3有图
     *
     * @param page
     * @param prodId
     * @param evaluate
     * @return
     */
    @Override
    public Page<ProdComm> getProdCommPageByProd(Page<ProdComm> page, Long prodId, Integer evaluate) {
        page = prodCommMapper.selectPage(page, new LambdaQueryWrapper<ProdComm>()
                //查询了所有的评论
                .eq(ProdComm::getProdId, prodId)
                //查中评和差评
                .eq(evaluate != -1 && evaluate != 3, ProdComm::getEvaluate, evaluate)
                //查询有图的
                .isNotNull(evaluate == 3, ProdComm::getPics));

        //去查询这些对应的评论人的头像和图片
        //前往member去查询
        List<ProdComm> records = page.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            List<String> userIds = records.stream().map(ProdComm::getUserId).collect(Collectors.toList());
            List<User> userList = prodToMemberFeign.loadUserById(userIds);
            //获得的id和use管理
            Map<String, List<User>> map = userList.stream().collect(Collectors.groupingBy(User::getUserId));
            records.stream().forEach(prodComm -> {
                //每个评论只有一个用户
                User user = map.get(prodComm.getUserId()).get(0);
                prodComm.setPic(user.getPic());
                prodComm.setNickName(user.getNickName());
            });
        }
        return page;
    }
}




